package com.item.service;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.item.dao.MajorMapper;
import com.item.dao.model.Major;
import com.item.dao.model.MajorExample;

@Service
public class MajorService {
    @Autowired
    private MajorMapper majorMapper;

    private static final Logger logger = Logger.getLogger(MajorService.class);

    public int countByExample(MajorExample example) {
        return this.majorMapper.countByExample(example);
    }

    public Major selectByPrimaryKey(String id) {
        return this.majorMapper.selectByPrimaryKey(id);
    }

    public List<Major> selectByExample(MajorExample example) {
        return this.majorMapper.selectByExample(example);
    }

    public int deleteByPrimaryKey(String id) {
        return this.majorMapper.deleteByPrimaryKey(id);
    }

    public int updateByPrimaryKeySelective(Major record) {
        return this.majorMapper.updateByPrimaryKeySelective(record);
    }

    public int updateByPrimaryKey(Major record) {
        return this.majorMapper.updateByPrimaryKey(record);
    }

    public int deleteByExample(MajorExample example) {
        return this.majorMapper.deleteByExample(example);
    }

    public int updateByExampleSelective(Major record, MajorExample example) {
        return this.majorMapper.updateByExampleSelective(record, example);
    }

    public int updateByExample(Major record, MajorExample example) {
        return this.majorMapper.updateByExample(record, example);
    }

    public int insert(Major record) {
        return this.majorMapper.insert(record);
    }

    public int insertSelective(Major record) {
        return this.majorMapper.insertSelective(record);
    }
    
    public String getLeastCode(String pid) {
    	String res = "M";
    	MajorExample example = new MajorExample();
    	example.createCriteria().andPidEqualTo(pid);
    	example.setOrderByClause("code ASC");
    	List<Major> list = this.majorMapper.selectByExample(example);
    	int i = 0;
    	for(int len = list.size(); i < 100 && i < len; i++) {
    		Major s = list.get(i);
    		int sCodeInt = Integer.parseInt(s.getCode().substring(1));
    		if(i < sCodeInt) {
    			break;
    		}
    	}
    	if(i == 0)
    		res += "01";
    	else
    		res += i < 10 ? "0" + i : i + "";
    	
    	return res;
    }
}